<template>
  <!-- 退货管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/returnProduct']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.name"
            placeholder="合同名称"
            clearable
          ></el-input>
          <el-select
            v-model="searchValue.status"
            placeholder="请选择退货状态"
            style="width: 100%"
            clearable
          >
            <el-option label="未完成" :value="0" />
            <el-option label="已完成" :value="1" />
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @row-dblclick="handleRowDblclick"
      >
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column label="标题" prop="name"> </el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column
          label="退货时间"
          prop="createTime"
          width="250"
        ></el-table-column>
        <el-table-column label="退货状态" prop="rejectedState" width="200">
          <template #default="scope">
            <!-- 退货完成状态：0未完成1完成 -->
            <p v-if="scope.row.rejectedState == 0">未完成</p>
            <p v-if="scope.row.rejectedState == 1" style="color: #67c23a">
              已完成
            </p>
          </template>
        </el-table-column>
        <el-table-column label="发货状态" prop="deliverStatus" width="200">
          <template #default="scope">
            <p v-if="scope.row.deliverStatus == 0">待发货</p>
            <p v-if="scope.row.deliverStatus == 1" style="color: green">
              已发货
            </p>
            <p v-if="scope.row.deliverStatus == 2" style="color: #2bd814">
              发货完成
            </p>
          </template>
        </el-table-column>
        <el-table-column
          label="审核状态"
          prop="status"
          width="150"
          fixed="right"
          sortable
        >
          <template #default="scope">
            <!--0，未审核，1,审核通过，2审核未通过-->
            <p v-if="scope.row.status == 0">待审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3" style="color: #ff9b31">审核中</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_cancel(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
                v-auth-button="['delete/returnProduct']"
                >取消退货</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_reCreate(scope.row, scope.$index)"
                v-if="scope.row.status == 1 && scope.row.rejectedState == 0"
                v-auth-button="['reCreate/returnProduct']"
                >重做</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_repair(scope.row, scope.$index)"
                v-if="scope.row.status == 1 && scope.row.rejectedState == 0"
                v-auth-button="['repair/returnProduct']"
                >维修</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_deliver(scope.row, scope.$index)"
                v-if="
                  scope.row.rejectedState == 1 && scope.row.deliverStatus != 2
                "
                v-auth-button="['send/produceOrderManagement']"
                >发货</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
                v-auth-button="['edit/returnProduct']"
                >编辑</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/returnProduct']"
                >查看</el-button
              >
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>
<script>

export default {
  name: 'returnGoods',
  data () {
    return {
      apiObj: this.$API.produce.returnProduct.getReturnProductList,
      searchValue: {
        name: null,
        status: null
      },
      selection: []

    }
  },

  methods: {
    //添加
    add () {
      this.$router.push({
        path: '/produce/returnProduct/addReturnProduct',
        query: {
          mode: 'add'
        }
      })
    },

    // 取消退货
    table_cancel (row) {
      this.$confirm(`此操作将取消【${row.name}】的退货, 请问是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await this.$API.produce.returnProduct.cancelReturnProduct.delete({ id: row.id })
        if (res.code == 200) {
          this.$refs.table.refresh()
          this.$message.success("操作成功")
        }
      }).catch(() => {

      })
    },

    // 发货
    table_deliver (row) {
      console.log(row)
      // source,//来源 0合同 1退货
      this.$router.push({
        path: '/produce/produceOrderManagement/deliverAdd',
        query: {
          mode: 'add',
          produceId: row.produceId,
          rejectedId: row.id,
          source: 1
        }
      })
    },

    // 重做
    table_reCreate (row) {
      this.$router.push({
        path: '/produce/returnProduct/reCreate',
        query: {
          id: row.id
        }
      })
    },

    // 维修
    table_repair (row) {
      this.$router.push({
        path: '/produce/returnProduct/repair',
        query: {
          id: row.id
        }
      })
    },

    //编辑
    table_edit (row) {
      this.$router.push({
        path: '/produce/returnProduct/addReturnProduct',
        query: {
          mode: 'edit',
          id: row.id
        }
      })
    },

    //查看
    table_show (row) {
      this.$router.push({
        path: '/produce/returnProduct/returnProductDetail',
        query: {
          id: row.id
        }
      })
    },


    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    }

  }
}
</script>

<style>
</style>